<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <title></title>
  <meta name="Generator" content="Cocoa HTML Writer">
  <meta name="CocoaVersion" content="824.42">
  <style type="text/css">
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
    p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
    p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
    p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
    p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
    p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
    p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #007300}
    span.s1 {color: #0000bf}
    span.s2 {color: #007300}
    span.s3 {color: #000000}
    span.s4 {color: #606060}
    span.Apple-tab-span {white-space:pre}
  </style>
</head>
<body>
<p class="p1"><b>PV_MagBuffer<span class="Apple-tab-span">	</span><span class="Apple-tab-span">	</span><span class="Apple-tab-span">	</span>Store FFT data in another buffer for other use</b></p>
<p class="p1"><b>PV_FreqBuffer</b></p>
<p class="p2"><br></p>
<p class="p3"><b>PV_MagBuffer(buffer, databuffer)</b></p>
<p class="p3"><b>PV_FreqBuffer(buffer, databuffer)</b></p>
<p class="p4"><b></b><br></p>
<p class="p3"><b><span class="Apple-tab-span">	</span>databuffer - a buffer of (fft buffer size / 2) for storing freq or mag data in</b></p>
<p class="p4"><b><span class="Apple-tab-span">	</span></b></p>
<p class="p3"><b>Examples:</b></p>
<p class="p2"><br></p>
<p class="p5">// stores the Mag values from an FFT analysis into a buffer to be used outside the FFT process</p>
<p class="p6">(</p>
<p class="p6"><span class="s1">SynthDef</span>(<span class="s2">\magbufferana</span>, {<span class="s1">arg</span> fftbuf, sndbuf, magbuf, freqbuf;</p>
<p class="p6"><span class="Apple-tab-span">	</span><span class="s1">var</span> in, chain;</p>
<p class="p6"><span class="Apple-tab-span">	</span>in = <span class="s1">PlayBuf</span>.ar(1, sndbuf, <span class="s1">BufRateScale</span>.kr(sndbuf), loop: 1);</p>
<p class="p6"><span class="Apple-tab-span">	</span>chain = <span class="s1">FFT</span>(fftbuf, in);</p>
<p class="p6"><span class="Apple-tab-span">	</span>chain = <span class="s1">PV_MagBuffer</span>(chain, magbuf);</p>
<p class="p6"><span class="Apple-tab-span">	</span>chain = <span class="s1">PV_FreqBuffer</span>(chain, freqbuf);</p>
<p class="p6"><span class="Apple-tab-span">	</span><span class="s1">IFFT</span>(chain);</p>
<p class="p6"><span class="Apple-tab-span">	</span>}).load(s);</p>
<p class="p2"><span class="Apple-tab-span">	</span></p>
<p class="p6"><span class="s1">SynthDef</span>(<span class="s2">\magbuffersynth</span>, {<span class="s1">arg</span> gate = 1, magbuf, freqbuf, fftframes, scale = 1;</p>
<p class="p6"><span class="Apple-tab-span">	</span><span class="s1">var</span> pars, src, out, trig, env;</p>
<p class="p6"><span class="Apple-tab-span">	</span>env = <span class="s1">EnvGen</span>.kr(<span class="s1">Env</span>([0, 1, 0], [1, 1], <span class="s2">\sin</span>, 1), gate, doneAction: 2);</p>
<p class="p5"><span class="s3"><span class="Apple-tab-span">	</span></span>// we'll grab 50 random partials</p>
<p class="p6"><span class="Apple-tab-span">	</span>pars = <span class="s1">Array</span>.fill(50, {<span class="s1">IRand</span>.new(0, fftframes)});</p>
<p class="p5"><span class="s3"><span class="Apple-tab-span">	</span></span>// use PinkNoise as a filter src</p>
<p class="p6"><span class="Apple-tab-span">	</span>src = <span class="s1">PinkNoise</span>.ar(0.5);</p>
<p class="p5"><span class="s3"><span class="Apple-tab-span">	</span></span>// Index the freq and mag buffers to control a filter bank</p>
<p class="p6"><span class="Apple-tab-span">	</span>out = (<span class="s1">BPF</span>.ar(src, <span class="s1">Index</span>.kr(freqbuf, pars), 0.001) * <span class="s1">Index</span>.kr(magbuf, pars)).sum * scale;</p>
<p class="p6"><span class="Apple-tab-span">	</span><span class="s1">Out</span>.ar(0, out * env);</p>
<p class="p6"><span class="Apple-tab-span">	</span>}).load(s);</p>
<p class="p6">)</p>
<p class="p6">s.boot;</p>
<p class="p2"><br></p>
<p class="p5">// allocate the fft buf, sndbuf, and mag and freq bufs</p>
<p class="p6">(</p>
<p class="p6">s.sendBundle(0.1, [<span class="s2">\b_alloc</span>, 0, 1024], [<span class="s2">\b_allocRead</span>, 1, <span class="s4">"sounds/a11wlk01.wav"</span>], [<span class="s2">\b_alloc</span>, 2, 512],<span class="Apple-converted-space"> </span></p>
<p class="p6"><span class="Apple-tab-span">	</span>[<span class="s2">\b_alloc</span>, 3, 512]);</p>
<p class="p6">)</p>
<p class="p5">// start the analuysis</p>
<p class="p6">(</p>
<p class="p6">s.sendMsg(<span class="s2">\s_new</span>, <span class="s2">\magbufferana</span>, z = s.nextNodeID, 0, 1, <span class="s2">\fftbuf</span>, 0, <span class="s2">\sndbuf</span>, 1,</p>
<p class="p7"><span class="s3"><span class="Apple-tab-span">	</span></span>\magbuf<span class="s3">, 2, </span>\freqbuf<span class="s3">, 3);</span></p>
<p class="p6">)</p>
<p class="p6">(</p>
<p class="p5">// there is a strange buildup of energy when a note starts... so, start the synthesis a bit later</p>
<p class="p6">s.sendMsg(<span class="s2">\s_new</span>, <span class="s2">\magbuffersynth</span>, y = s.nextNodeID, 1, 1, <span class="s2">\magbuf</span>, 2, <span class="s2">\freqbuf</span>, 3,<span class="Apple-converted-space"> </span></p>
<p class="p7"><span class="s3"><span class="Apple-tab-span">	</span></span>\fftframes<span class="s3">, 512);</span></p>
<p class="p6">)</p>
<p class="p5"><span class="s3">( </span>// kill the current note, start a new one (with different pars)</p>
<p class="p6">s.sendMsg(<span class="s2">\n_set</span>, y, <span class="s2">\gate</span>, 0);</p>
<p class="p6">s.sendMsg(<span class="s2">\s_new</span>, <span class="s2">\magbuffersynth</span>, y = s.nextNodeID, 1, 1, <span class="s2">\magbuf</span>, 2, <span class="s2">\freqbuf</span>, 3,<span class="Apple-converted-space"> </span></p>
<p class="p7"><span class="s3"><span class="Apple-tab-span">	</span></span>\fftframes<span class="s3">, 512)</span></p>
<p class="p6">)</p>
<p class="p6">// free the synthesis synth</p>
<p class="p6">s.sendMsg(<span class="s2">\n_set</span>, y, <span class="s2">\gate</span>, 0)</p>
<p class="p5">// free the analysis</p>
<p class="p6">s.sendMsg(<span class="s2">\n_free</span>, z);</p>
<p class="p2"><br></p>
<p class="p6">// free the buffers</p>
<p class="p6">4.do{arg i; s.sendMsg(\b_free, i)};</p>
<p class="p2"><br></p>
<p class="p6">s.quit</p>
</body>
</html>
